<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>定义模型 | 欢迎学习django课程</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../part2/3.html" />
    
    
    <link rel="prev" href="../part2/1.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="2.1"
        data-chapter-title="定义模型"
        data-filepath="part2/2.md"
        data-basepath=".."
        data-revision="Tue Jan 03 2017 08:43:08 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        课程介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="part1/1.html">
            
                
                    <a href="../part1/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        入门
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="part1/2.html">
            
                
                    <a href="../part1/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        搭建开发环境
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="part1/3.html">
            
                
                    <a href="../part1/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        设计模型
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="part1/4.html">
            
                
                    <a href="../part1/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        管理站点
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="part1/5.html">
            
                
                    <a href="../part1/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        视图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="part1/6.html">
            
                
                    <a href="../part1/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        模板
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="part1/7.html">
            
                
                    <a href="../part1/7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="part2/1.html">
            
                
                    <a href="../part2/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        模型
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="2.1" data-path="part2/2.html">
            
                
                    <a href="../part2/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        定义模型
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="part2/3.html">
            
                
                    <a href="../part2/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        模型成员
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="part2/4.html">
            
                
                    <a href="../part2/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        模型查询
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="part2/5.html">
            
                
                    <a href="../part2/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        自连接
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="part2/6.html">
            
                
                    <a href="../part2/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="part3/1.html">
            
                
                    <a href="../part3/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        视图
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="part3/2.html">
            
                
                    <a href="../part3/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        URLconf
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="part3/3.html">
            
                
                    <a href="../part3/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        视图函数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="part3/4.html">
            
                
                    <a href="../part3/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        Request对象
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.3.1" data-path="part3/4_1.html">
            
                
                    <a href="../part3/4_1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.1.</b>
                        
                        QueryDict对象
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3.2" data-path="part3/4_2.html">
            
                
                    <a href="../part3/4_2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.2.</b>
                        
                        GET属性
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3.3" data-path="part3/4_3.html">
            
                
                    <a href="../part3/4_3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.3.</b>
                        
                        POST属性
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="part3/5.html">
            
                
                    <a href="../part3/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        Response对象
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="part3/6.html">
            
                
                    <a href="../part3/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                        状态保持
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="part3/7.html">
            
                
                    <a href="../part3/7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.6.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="part4/1.html">
            
                
                    <a href="../part4/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        模板
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="part4/2.html">
            
                
                    <a href="../part4/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        定义模板
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="part4/3.html">
            
                
                    <a href="../part4/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        模板继承
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="part4/4.html">
            
                
                    <a href="../part4/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        HTML转义
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="part4/5.html">
            
                
                    <a href="../part4/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        CSRF
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="part4/6.html">
            
                
                    <a href="../part4/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        验证码
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.6" data-path="part4/7.html">
            
                
                    <a href="../part4/7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.6.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="part5/1.html">
            
                
                    <a href="../part5/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        高级
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="part5/2.html">
            
                
                    <a href="../part5/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        静态文件处理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="part5/3.html">
            
                
                    <a href="../part5/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        中间件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="part5/4.html">
            
                
                    <a href="../part5/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        上传图片
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="part5/5.html">
            
                
                    <a href="../part5/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.4.</b>
                        
                        Admin站点
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="part5/6.html">
            
                
                    <a href="../part5/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.5.</b>
                        
                        分页
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="part5/7.html">
            
                
                    <a href="../part5/7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.6.</b>
                        
                        ajax
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.7" data-path="part5/8.html">
            
                
                    <a href="../part5/8.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.7.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="part6/1.html">
            
                
                    <a href="../part6/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        第三方
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="part6/2.html">
            
                
                    <a href="../part6/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        富文本编辑器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="part6/3.html">
            
                
                    <a href="../part6/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        缓存
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="part6/4.html">
            
                
                    <a href="../part6/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        全文检索
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="part6/5.html">
            
                
                    <a href="../part6/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        celery
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.5" data-path="part6/6.html">
            
                
                    <a href="../part6/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.5.</b>
                        
                        布署
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.6" data-path="part6/7.html">
            
                
                    <a href="../part6/7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.6.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7" data-path="part7/1.html">
            
                
                    <a href="../part7/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        Git与项目
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1" data-path="part7/2.html">
            
                
                    <a href="../part7/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.</b>
                        
                        Git简介
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1.1" data-path="part7/2_2.html">
            
                
                    <a href="../part7/2_2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.1.</b>
                        
                        远程仓库
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.1.2" data-path="part7/2_1.html">
            
                
                    <a href="../part7/2_1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.2.</b>
                        
                        本地仓库
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="part7/3.html">
            
                
                    <a href="../part7/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.2.</b>
                        
                        项目设计
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.3" data-path="part7/4.html">
            
                
                    <a href="../part7/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.3.</b>
                        
                        模型类设计
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.4" data-path="part7/5.html">
            
                
                    <a href="../part7/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.4.</b>
                        
                        使用页面
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.5" data-path="part7/6.html">
            
                
                    <a href="../part7/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.5.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >欢迎学习django课程</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x5B9A;&#x4E49;&#x6A21;&#x578B;">&#x5B9A;&#x4E49;&#x6A21;&#x578B;</h1>
<ul>
<li>&#x5728;&#x6A21;&#x578B;&#x4E2D;&#x5B9A;&#x4E49;&#x5C5E;&#x6027;&#xFF0C;&#x4F1A;&#x751F;&#x6210;&#x8868;&#x4E2D;&#x7684;&#x5B57;&#x6BB5;</li>
<li>django&#x6839;&#x636E;&#x5C5E;&#x6027;&#x7684;&#x7C7B;&#x578B;&#x786E;&#x5B9A;&#x4EE5;&#x4E0B;&#x4FE1;&#x606F;&#xFF1A;<ul>
<li>&#x5F53;&#x524D;&#x9009;&#x62E9;&#x7684;&#x6570;&#x636E;&#x5E93;&#x652F;&#x6301;&#x5B57;&#x6BB5;&#x7684;&#x7C7B;&#x578B;</li>
<li>&#x6E32;&#x67D3;&#x7BA1;&#x7406;&#x8868;&#x5355;&#x65F6;&#x4F7F;&#x7528;&#x7684;&#x9ED8;&#x8BA4;html&#x63A7;&#x4EF6;</li>
<li>&#x5728;&#x7BA1;&#x7406;&#x7AD9;&#x70B9;&#x6700;&#x4F4E;&#x9650;&#x5EA6;&#x7684;&#x9A8C;&#x8BC1;</li>
</ul>
</li>
<li>django&#x4F1A;&#x4E3A;&#x8868;&#x589E;&#x52A0;&#x81EA;&#x52A8;&#x589E;&#x957F;&#x7684;&#x4E3B;&#x952E;&#x5217;&#xFF0C;&#x6BCF;&#x4E2A;&#x6A21;&#x578B;&#x53EA;&#x80FD;&#x6709;&#x4E00;&#x4E2A;&#x4E3B;&#x952E;&#x5217;&#xFF0C;&#x5982;&#x679C;&#x4F7F;&#x7528;&#x9009;&#x9879;&#x8BBE;&#x7F6E;&#x67D0;&#x5C5E;&#x6027;&#x4E3A;&#x4E3B;&#x952E;&#x5217;&#x540E;&#xFF0C;&#x5219;django&#x4E0D;&#x4F1A;&#x518D;&#x751F;&#x6210;&#x9ED8;&#x8BA4;&#x7684;&#x4E3B;&#x952E;&#x5217;</li>
<li>&#x5C5E;&#x6027;&#x547D;&#x540D;&#x9650;&#x5236;<ul>
<li>&#x4E0D;&#x80FD;&#x662F;python&#x7684;&#x4FDD;&#x7559;&#x5173;&#x952E;&#x5B57;</li>
<li>&#x7531;&#x4E8E;django&#x7684;&#x67E5;&#x8BE2;&#x65B9;&#x5F0F;&#xFF0C;&#x4E0D;&#x5141;&#x8BB8;&#x4F7F;&#x7528;&#x8FDE;&#x7EED;&#x7684;&#x4E0B;&#x5212;&#x7EBF;</li>
</ul>
</li>
</ul>
<h1 id="&#x5B9A;&#x4E49;&#x5C5E;&#x6027;">&#x5B9A;&#x4E49;&#x5C5E;&#x6027;</h1>
<ul>
<li>&#x5B9A;&#x4E49;&#x5C5E;&#x6027;&#x65F6;&#xFF0C;&#x9700;&#x8981;&#x5B57;&#x6BB5;&#x7C7B;&#x578B;</li>
<li>&#x5B57;&#x6BB5;&#x7C7B;&#x578B;&#x88AB;&#x5B9A;&#x4E49;&#x5728;django.db.models.fields&#x76EE;&#x5F55;&#x4E0B;&#xFF0C;&#x4E3A;&#x4E86;&#x65B9;&#x4FBF;&#x4F7F;&#x7528;&#xFF0C;&#x88AB;&#x5BFC;&#x5165;&#x5230;django.db.models&#x4E2D;</li>
<li>&#x4F7F;&#x7528;&#x65B9;&#x5F0F;<ol>
<li>&#x5BFC;&#x5165;from django.db import models</li>
<li>&#x901A;&#x8FC7;models.<Foo>Field&#x521B;&#x5EFA;&#x5B57;&#x6BB5;&#x7C7B;&#x578B;&#x7684;&#x5BF9;&#x8C61;&#xFF0C;&#x8D4B;&#x503C;&#x7ED9;&#x5C5E;&#x6027;</Foo></li>
</ol>
</li>
<li><strong>&#x5BF9;&#x4E8E;&#x91CD;&#x8981;&#x6570;&#x636E;&#x90FD;&#x505A;&#x903B;&#x8F91;&#x5220;&#x9664;&#xFF0C;&#x4E0D;&#x505A;&#x7269;&#x7406;&#x5220;&#x9664;&#xFF0C;&#x5B9E;&#x73B0;&#x65B9;&#x6CD5;&#x662F;&#x5B9A;&#x4E49;isDelete&#x5C5E;&#x6027;&#xFF0C;&#x7C7B;&#x578B;&#x4E3A;BooleanField&#xFF0C;&#x9ED8;&#x8BA4;&#x503C;&#x4E3A;False</strong></li>
</ul>
<h4 id="&#x5B57;&#x6BB5;&#x7C7B;&#x578B;">&#x5B57;&#x6BB5;&#x7C7B;&#x578B;</h4>
<ul>
<li>AutoField&#xFF1A;&#x4E00;&#x4E2A;&#x6839;&#x636E;&#x5B9E;&#x9645;ID&#x81EA;&#x52A8;&#x589E;&#x957F;&#x7684;IntegerField&#xFF0C;&#x901A;&#x5E38;&#x4E0D;&#x6307;&#x5B9A;<ul>
<li>&#x5982;&#x679C;&#x4E0D;&#x6307;&#x5B9A;&#xFF0C;&#x4E00;&#x4E2A;&#x4E3B;&#x952E;&#x5B57;&#x6BB5;&#x5C06;&#x81EA;&#x52A8;&#x6DFB;&#x52A0;&#x5230;&#x6A21;&#x578B;&#x4E2D;</li>
</ul>
</li>
<li>BooleanField&#xFF1A;true/false &#x5B57;&#x6BB5;&#xFF0C;&#x6B64;&#x5B57;&#x6BB5;&#x7684;&#x9ED8;&#x8BA4;&#x8868;&#x5355;&#x63A7;&#x5236;&#x662F;CheckboxInput</li>
<li>NullBooleanField&#xFF1A;&#x652F;&#x6301;null&#x3001;true&#x3001;false&#x4E09;&#x79CD;&#x503C;</li>
<li>CharField(max_length=&#x5B57;&#x7B26;&#x957F;&#x5EA6;)&#xFF1A;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x9ED8;&#x8BA4;&#x7684;&#x8868;&#x5355;&#x6837;&#x5F0F;&#x662F; TextInput</li>
<li>TextField&#xFF1A;&#x5927;&#x6587;&#x672C;&#x5B57;&#x6BB5;&#xFF0C;&#x4E00;&#x822C;&#x8D85;&#x8FC7;4000&#x4F7F;&#x7528;&#xFF0C;&#x9ED8;&#x8BA4;&#x7684;&#x8868;&#x5355;&#x63A7;&#x4EF6;&#x662F;Textarea</li>
<li>IntegerField&#xFF1A;&#x6574;&#x6570;</li>
<li>DecimalField(max_digits=None, decimal_places=None)&#xFF1A;&#x4F7F;&#x7528;python&#x7684;Decimal&#x5B9E;&#x4F8B;&#x8868;&#x793A;&#x7684;&#x5341;&#x8FDB;&#x5236;&#x6D6E;&#x70B9;&#x6570;<ul>
<li>DecimalField.max_digits&#xFF1A;&#x4F4D;&#x6570;&#x603B;&#x6570;</li>
<li>DecimalField.decimal_places&#xFF1A;&#x5C0F;&#x6570;&#x70B9;&#x540E;&#x7684;&#x6570;&#x5B57;&#x4F4D;&#x6570;</li>
</ul>
</li>
<li>FloatField&#xFF1A;&#x7528;Python&#x7684;float&#x5B9E;&#x4F8B;&#x6765;&#x8868;&#x793A;&#x7684;&#x6D6E;&#x70B9;&#x6570;</li>
<li>DateField[auto_now=False, auto_now_add=False])&#xFF1A;&#x4F7F;&#x7528;Python&#x7684;datetime.date&#x5B9E;&#x4F8B;&#x8868;&#x793A;&#x7684;&#x65E5;&#x671F;<ul>
<li>&#x53C2;&#x6570;DateField.auto_now&#xFF1A;&#x6BCF;&#x6B21;&#x4FDD;&#x5B58;&#x5BF9;&#x8C61;&#x65F6;&#xFF0C;&#x81EA;&#x52A8;&#x8BBE;&#x7F6E;&#x8BE5;&#x5B57;&#x6BB5;&#x4E3A;&#x5F53;&#x524D;&#x65F6;&#x95F4;&#xFF0C;&#x7528;&#x4E8E;&quot;&#x6700;&#x540E;&#x4E00;&#x6B21;&#x4FEE;&#x6539;&quot;&#x7684;&#x65F6;&#x95F4;&#x6233;&#xFF0C;&#x5B83;&#x603B;&#x662F;&#x4F7F;&#x7528;&#x5F53;&#x524D;&#x65E5;&#x671F;&#xFF0C;&#x9ED8;&#x8BA4;&#x4E3A;false</li>
<li>&#x53C2;&#x6570;DateField.auto_now_add&#xFF1A;&#x5F53;&#x5BF9;&#x8C61;&#x7B2C;&#x4E00;&#x6B21;&#x88AB;&#x521B;&#x5EFA;&#x65F6;&#x81EA;&#x52A8;&#x8BBE;&#x7F6E;&#x5F53;&#x524D;&#x65F6;&#x95F4;&#xFF0C;&#x7528;&#x4E8E;&#x521B;&#x5EFA;&#x7684;&#x65F6;&#x95F4;&#x6233;&#xFF0C;&#x5B83;&#x603B;&#x662F;&#x4F7F;&#x7528;&#x5F53;&#x524D;&#x65E5;&#x671F;&#xFF0C;&#x9ED8;&#x8BA4;&#x4E3A;false</li>
<li>&#x8BE5;&#x5B57;&#x6BB5;&#x9ED8;&#x8BA4;&#x5BF9;&#x5E94;&#x7684;&#x8868;&#x5355;&#x63A7;&#x4EF6;&#x662F;&#x4E00;&#x4E2A;TextInput. &#x5728;&#x7BA1;&#x7406;&#x5458;&#x7AD9;&#x70B9;&#x6DFB;&#x52A0;&#x4E86;&#x4E00;&#x4E2A;JavaScript&#x5199;&#x7684;&#x65E5;&#x5386;&#x63A7;&#x4EF6;&#xFF0C;&#x548C;&#x4E00;&#x4E2A;&#x201C;Today&quot;&#x7684;&#x5FEB;&#x6377;&#x6309;&#x94AE;&#xFF0C;&#x5305;&#x542B;&#x4E86;&#x4E00;&#x4E2A;&#x989D;&#x5916;&#x7684;invalid_date&#x9519;&#x8BEF;&#x6D88;&#x606F;&#x952E;</li>
<li>auto_now_add, auto_now, and default &#x8FD9;&#x4E9B;&#x8BBE;&#x7F6E;&#x662F;&#x76F8;&#x4E92;&#x6392;&#x65A5;&#x7684;&#xFF0C;&#x4ED6;&#x4EEC;&#x4E4B;&#x95F4;&#x7684;&#x4EFB;&#x4F55;&#x7EC4;&#x5408;&#x5C06;&#x4F1A;&#x53D1;&#x751F;&#x9519;&#x8BEF;&#x7684;&#x7ED3;&#x679C;</li>
</ul>
</li>
<li>TimeField&#xFF1A;&#x4F7F;&#x7528;Python&#x7684;datetime.time&#x5B9E;&#x4F8B;&#x8868;&#x793A;&#x7684;&#x65F6;&#x95F4;&#xFF0C;&#x53C2;&#x6570;&#x540C;DateField</li>
<li>DateTimeField&#xFF1A;&#x4F7F;&#x7528;Python&#x7684;datetime.datetime&#x5B9E;&#x4F8B;&#x8868;&#x793A;&#x7684;&#x65E5;&#x671F;&#x548C;&#x65F6;&#x95F4;&#xFF0C;&#x53C2;&#x6570;&#x540C;DateField</li>
<li>FileField&#xFF1A;&#x4E00;&#x4E2A;&#x4E0A;&#x4F20;&#x6587;&#x4EF6;&#x7684;&#x5B57;&#x6BB5;</li>
<li>ImageField&#xFF1A;&#x7EE7;&#x627F;&#x4E86;FileField&#x7684;&#x6240;&#x6709;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;&#xFF0C;&#x4F46;&#x5BF9;&#x4E0A;&#x4F20;&#x7684;&#x5BF9;&#x8C61;&#x8FDB;&#x884C;&#x6821;&#x9A8C;&#xFF0C;&#x786E;&#x4FDD;&#x5B83;&#x662F;&#x4E2A;&#x6709;&#x6548;&#x7684;image</li>
</ul>
<h4 id="&#x5B57;&#x6BB5;&#x9009;&#x9879;">&#x5B57;&#x6BB5;&#x9009;&#x9879;</h4>
<ul>
<li>&#x901A;&#x8FC7;&#x5B57;&#x6BB5;&#x9009;&#x9879;&#xFF0C;&#x53EF;&#x4EE5;&#x5B9E;&#x73B0;&#x5BF9;&#x5B57;&#x6BB5;&#x7684;&#x7EA6;&#x675F;</li>
<li>&#x5728;&#x5B57;&#x6BB5;&#x5BF9;&#x8C61;&#x65F6;&#x901A;&#x8FC7;&#x5173;&#x952E;&#x5B57;&#x53C2;&#x6570;&#x6307;&#x5B9A;</li>
<li>null&#xFF1A;&#x5982;&#x679C;&#x4E3A;True&#xFF0C;Django &#x5C06;&#x7A7A;&#x503C;&#x4EE5;NULL &#x5B58;&#x50A8;&#x5230;&#x6570;&#x636E;&#x5E93;&#x4E2D;&#xFF0C;&#x9ED8;&#x8BA4;&#x503C;&#x662F; False</li>
<li>blank&#xFF1A;&#x5982;&#x679C;&#x4E3A;True&#xFF0C;&#x5219;&#x8BE5;&#x5B57;&#x6BB5;&#x5141;&#x8BB8;&#x4E3A;&#x7A7A;&#x767D;&#xFF0C;&#x9ED8;&#x8BA4;&#x503C;&#x662F; False</li>
<li><strong>&#x5BF9;&#x6BD4;&#xFF1A;null&#x662F;&#x6570;&#x636E;&#x5E93;&#x8303;&#x7574;&#x7684;&#x6982;&#x5FF5;&#xFF0C;blank&#x662F;&#x8868;&#x5355;&#x9A8C;&#x8BC1;&#x8BC1;&#x8303;&#x7574;&#x7684;</strong></li>
<li>db_column&#xFF1A;&#x5B57;&#x6BB5;&#x7684;&#x540D;&#x79F0;&#xFF0C;&#x5982;&#x679C;&#x672A;&#x6307;&#x5B9A;&#xFF0C;&#x5219;&#x4F7F;&#x7528;&#x5C5E;&#x6027;&#x7684;&#x540D;&#x79F0;</li>
<li>db_index&#xFF1A;&#x82E5;&#x503C;&#x4E3A; True, &#x5219;&#x5728;&#x8868;&#x4E2D;&#x4F1A;&#x4E3A;&#x6B64;&#x5B57;&#x6BB5;&#x521B;&#x5EFA;&#x7D22;&#x5F15;</li>
<li>default&#xFF1A;&#x9ED8;&#x8BA4;&#x503C;</li>
<li>primary_key&#xFF1A;&#x82E5;&#x4E3A; True, &#x5219;&#x8BE5;&#x5B57;&#x6BB5;&#x4F1A;&#x6210;&#x4E3A;&#x6A21;&#x578B;&#x7684;&#x4E3B;&#x952E;&#x5B57;&#x6BB5;</li>
<li>unique&#xFF1A;&#x5982;&#x679C;&#x4E3A; True, &#x8FD9;&#x4E2A;&#x5B57;&#x6BB5;&#x5728;&#x8868;&#x4E2D;&#x5FC5;&#x987B;&#x6709;&#x552F;&#x4E00;&#x503C;</li>
</ul>
<h4 id="&#x5173;&#x7CFB;">&#x5173;&#x7CFB;</h4>
<ul>
<li>&#x5173;&#x7CFB;&#x7684;&#x7C7B;&#x578B;&#x5305;&#x62EC;<ul>
<li>ForeignKey&#xFF1A;&#x4E00;&#x5BF9;&#x591A;&#xFF0C;&#x5C06;&#x5B57;&#x6BB5;&#x5B9A;&#x4E49;&#x5728;&#x591A;&#x7684;&#x7AEF;&#x4E2D;</li>
<li>ManyToManyField&#xFF1A;&#x591A;&#x5BF9;&#x591A;&#xFF0C;&#x5C06;&#x5B57;&#x6BB5;&#x5B9A;&#x4E49;&#x5728;&#x4E24;&#x7AEF;&#x4E2D;</li>
<li>OneToOneField&#xFF1A;&#x4E00;&#x5BF9;&#x4E00;&#xFF0C;&#x5C06;&#x5B57;&#x6BB5;&#x5B9A;&#x4E49;&#x5728;&#x4EFB;&#x610F;&#x4E00;&#x7AEF;&#x4E2D;</li>
</ul>
</li>
<li>&#x53EF;&#x4EE5;&#x7EF4;&#x62A4;&#x9012;&#x5F52;&#x7684;&#x5173;&#x8054;&#x5173;&#x7CFB;&#xFF0C;&#x4F7F;&#x7528;&apos;self&apos;&#x6307;&#x5B9A;&#xFF0C;&#x8BE6;&#x89C1;&#x201C;&#x81EA;&#x5173;&#x8054;&#x201D;</li>
<li>&#x7528;&#x4E00;&#x8BBF;&#x95EE;&#x591A;&#xFF1A;&#x5BF9;&#x8C61;.&#x6A21;&#x578B;&#x7C7B;&#x5C0F;&#x5199;_set</li>
</ul>
<pre><code>bookinfo.heroinfo_set
</code></pre><ul>
<li>&#x7528;&#x4E00;&#x8BBF;&#x95EE;&#x4E00;&#xFF1A;&#x5BF9;&#x8C61;.&#x6A21;&#x578B;&#x7C7B;&#x5C0F;&#x5199;</li>
</ul>
<pre><code>heroinfo.bookinfo
</code></pre><ul>
<li>&#x8BBF;&#x95EE;id&#xFF1A;&#x5BF9;&#x8C61;.&#x5C5E;&#x6027;_id</li>
</ul>
<pre><code>heroinfo.book_id
</code></pre><h1 id="&#x5143;&#x9009;&#x9879;">&#x5143;&#x9009;&#x9879;</h1>
<ul>
<li>&#x5728;&#x6A21;&#x578B;&#x7C7B;&#x4E2D;&#x5B9A;&#x4E49;&#x7C7B;Meta&#xFF0C;&#x7528;&#x4E8E;&#x8BBE;&#x7F6E;&#x5143;&#x4FE1;&#x606F;</li>
<li>&#x5143;&#x4FE1;&#x606F;db_table&#xFF1A;&#x5B9A;&#x4E49;&#x6570;&#x636E;&#x8868;&#x540D;&#x79F0;&#xFF0C;&#x63A8;&#x8350;&#x4F7F;&#x7528;&#x5C0F;&#x5199;&#x5B57;&#x6BCD;&#xFF0C;&#x6570;&#x636E;&#x8868;&#x7684;&#x9ED8;&#x8BA4;&#x540D;&#x79F0;</li>
</ul>
<pre><code>&lt;app_name&gt;_&lt;model_name&gt;
</code></pre><ul>
<li>ordering&#xFF1A;&#x5BF9;&#x8C61;&#x7684;&#x9ED8;&#x8BA4;&#x6392;&#x5E8F;&#x5B57;&#x6BB5;&#xFF0C;&#x83B7;&#x53D6;&#x5BF9;&#x8C61;&#x7684;&#x5217;&#x8868;&#x65F6;&#x4F7F;&#x7528;&#xFF0C;&#x63A5;&#x6536;&#x5C5E;&#x6027;&#x6784;&#x6210;&#x7684;&#x5217;&#x8868;</li>
</ul>
<pre><code>class BookInfo(models.Model):
    ...
    class Meta():
        ordering = [&apos;id&apos;]
</code></pre><ul>
<li>&#x5B57;&#x7B26;&#x4E32;&#x524D;&#x52A0;-&#x8868;&#x793A;&#x5012;&#x5E8F;&#xFF0C;&#x4E0D;&#x52A0;-&#x8868;&#x793A;&#x6B63;&#x5E8F;</li>
</ul>
<pre><code>class BookInfo(models.Model):
    ...
    class Meta():
        ordering = [&apos;-id&apos;]
</code></pre><ul>
<li>&#x6392;&#x5E8F;&#x4F1A;&#x589E;&#x52A0;&#x6570;&#x636E;&#x5E93;&#x7684;&#x5F00;&#x9500;</li>
</ul>
<h1 id="&#x793A;&#x4F8B;&#x6F14;&#x793A;">&#x793A;&#x4F8B;&#x6F14;&#x793A;</h1>
<ul>
<li>&#x521B;&#x5EFA;test2&#x9879;&#x76EE;&#xFF0C;&#x5E76;&#x521B;&#x5EFA;booktest&#x5E94;&#x7528;&#xFF0C;&#x4F7F;&#x7528;mysql&#x6570;&#x636E;&#x5E93;</li>
<li>&#x5B9A;&#x4E49;&#x56FE;&#x4E66;&#x6A21;&#x578B;</li>
</ul>
<pre><code>class BookInfo(models.Model):
    btitle = models.CharField(max_length=20)
    bpub_date = models.DateTimeField()
    bread = models.IntegerField(default=0)
    bcommet = models.IntegerField(default=0)
    isDelete = models.BooleanField(default=False)
</code></pre><ul>
<li>&#x82F1;&#x96C4;&#x6A21;&#x578B;</li>
</ul>
<pre><code>class HeroInfo(models.Model):
    hname = models.CharField(max_length=20)
    hgender = models.BooleanField(default=True)
    isDelete = models.BooleanField(default=False)
    hcontent = models.CharField(max_length=100)
    hbook = models.ForeignKey(&apos;BookInfo&apos;)
</code></pre><ul>
<li>&#x5B9A;&#x4E49;index&#x3001;detail&#x89C6;&#x56FE;</li>
<li>index.html&#x3001;detail.html&#x6A21;&#x677F;</li>
<li>&#x914D;&#x7F6E;url&#xFF0C;&#x80FD;&#x591F;&#x5B8C;&#x6210;&#x56FE;&#x4E66;&#x53CA;&#x82F1;&#x96C4;&#x7684;&#x5C55;&#x793A;</li>
</ul>
<h1 id="&#x6D4B;&#x8BD5;&#x6570;&#x636E;">&#x6D4B;&#x8BD5;&#x6570;&#x636E;</h1>
<ul>
<li>&#x6A21;&#x578B;BookInfo&#x7684;&#x6D4B;&#x8BD5;&#x6570;&#x636E;</li>
</ul>
<pre><code>insert into booktest_bookinfo(btitle,bpub_date,bread,bcommet,isDelete) values
(&apos;&#x5C04;&#x96D5;&#x82F1;&#x96C4;&#x4F20;&apos;,&apos;1980-5-1&apos;,12,34,0),
(&apos;&#x5929;&#x9F99;&#x516B;&#x90E8;&apos;,&apos;1986-7-24&apos;,36,40,0),
(&apos;&#x7B11;&#x50B2;&#x6C5F;&#x6E56;&apos;,&apos;1995-12-24&apos;,20,80,0),
(&apos;&#x96EA;&#x5C71;&#x98DE;&#x72D0;&apos;,&apos;1987-11-11&apos;,58,24,0)
</code></pre><ul>
<li>&#x6A21;&#x578B;HeroInfo&#x7684;&#x6D4B;&#x8BD5;&#x6570;&#x636E;</li>
</ul>
<pre><code>insert into booktest_heroinfo(hname,hgender,hbook_id,hcontent,isDelete) values
(&apos;&#x90ED;&#x9756;&apos;,1,1,&apos;&#x964D;&#x9F99;&#x5341;&#x516B;&#x638C;&apos;,0),
(&apos;&#x9EC4;&#x84C9;&apos;,0,1,&apos;&#x6253;&#x72D7;&#x68CD;&#x6CD5;&apos;,0),
(&apos;&#x9EC4;&#x836F;&#x5E08;&apos;,1,1,&apos;&#x5F39;&#x6307;&#x795E;&#x901A;&apos;,0),
(&apos;&#x6B27;&#x9633;&#x950B;&apos;,1,1,&apos;&#x86E4;&#x87C6;&#x529F;&apos;,0),
(&apos;&#x6885;&#x8D85;&#x98CE;&apos;,0,1,&apos;&#x4E5D;&#x9634;&#x767D;&#x9AA8;&#x722A;&apos;,0),
(&apos;&#x4E54;&#x5CF0;&apos;,1,2,&apos;&#x964D;&#x9F99;&#x5341;&#x516B;&#x638C;&apos;,0),
(&apos;&#x6BB5;&#x8A89;&apos;,1,2,&apos;&#x516D;&#x8109;&#x795E;&#x5251;&apos;,0),
(&apos;&#x865A;&#x7AF9;&apos;,1,2,&apos;&#x5929;&#x5C71;&#x516D;&#x9633;&#x638C;&apos;,0),
(&apos;&#x738B;&#x8BED;&#x5AE3;&apos;,0,2,&apos;&#x795E;&#x4ED9;&#x59D0;&#x59D0;&apos;,0),
(&apos;&#x4EE4;&#x72D0;&#x51B2;&apos;,1,3,&apos;&#x72EC;&#x5B64;&#x4E5D;&#x5251;&apos;,0),
(&apos;&#x4EFB;&#x76C8;&#x76C8;&apos;,0,3,&apos;&#x5F39;&#x7434;&apos;,0),
(&apos;&#x5CB3;&#x4E0D;&#x7FA4;&apos;,1,3,&apos;&#x534E;&#x5C71;&#x5251;&#x6CD5;&apos;,0),
(&apos;&#x4E1C;&#x65B9;&#x4E0D;&#x8D25;&apos;,0,3,&apos;&#x8475;&#x82B1;&#x5B9D;&#x5178;&apos;,0),
(&apos;&#x80E1;&#x6590;&apos;,1,4,&apos;&#x80E1;&#x5BB6;&#x5200;&#x6CD5;&apos;,0),
(&apos;&#x82D7;&#x82E5;&#x5170;&apos;,0,4,&apos;&#x9EC4;&#x8863;&apos;,0),
(&apos;&#x7A0B;&#x7075;&#x7D20;&apos;,0,4,&apos;&#x533B;&#x672F;&apos;,0),
(&apos;&#x8881;&#x7D2B;&#x8863;&apos;,0,4,&apos;&#x516D;&#x5408;&#x62F3;&apos;,0)
</code></pre>
                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../part2/1.html" class="navigation navigation-prev " aria-label="Previous page: 模型"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../part2/3.html" class="navigation navigation-next " aria-label="Next page: 模型成员"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
